package com.ddbook.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ddbook.JwtUtil;
import com.ddbook.VO.ReadersVO;
import com.ddbook.enums.UserEnum;
import com.ddbook.mapper.AuthorMapper;
import com.ddbook.model.Author;
import com.ddbook.VO.AuthorVO;
import com.ddbook.model.Readers;
import com.ddbook.result.Result;
import com.ddbook.result.ResultConstant;
import com.ddbook.service.ReadersService;
import com.ddbook.token.UserToken;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.junit.jupiter.api.Test;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootTest
@RestController
@RequestMapping("login")
public class LoginController {

    @Autowired
    private AuthorMapper authorMapper;

    @Autowired
    private JwtUtil jwtUtil;

    @Test
    public void addUser(){
        Author author = new Author();
        author.setName("李四");
        authorMapper.insert(author);
    }

    @Test
    public void selectByName(){
        QueryWrapper<Author> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username","唐家三少");
        authorMapper.selectOne(queryWrapper);
    }

    //作者登录
    @RequestMapping("/AuthorLogin")
    public Result AuthorLogin(String username,String password){
        //获取subject
        Subject subject = SecurityUtils.getSubject();
        //封装数据
        UserToken userToken = new UserToken(username, password, UserEnum.ADMIN);
        //认证
        try {
            subject.login(userToken);
        } catch (UnknownAccountException e) {
            e.printStackTrace();
            return new Result(ResultConstant.USERNAME_PASSWORD_ERROR_CODE, ResultConstant.USERNAME_PASSWORD_ERROR_DATA);
        }catch (IncorrectCredentialsException e){
            e.printStackTrace();
            return new Result(ResultConstant.USERNAME_PASSWORD_ERROR_CODE, ResultConstant.USERNAME_PASSWORD_ERROR_DATA);
        }
        //单点登录
        //获取当前用户
        Author tPerson = (Author) subject.getPrincipal();
        //脱敏处理
        AuthorVO vo = new AuthorVO();
        BeanUtils.copyProperties(tPerson,vo);
        //生成token
        String to = jwtUtil.createJwt(vo.getId().toString(), JSONObject.toJSONString(tPerson), "role");
        return new Result(ResultConstant.LOGIN_SUCCESS_CODE, ResultConstant.LOGIN_SUCCESS_DATA,to);
    }

    //读者登录
    @RequestMapping("/ReadersLogin")
    public Result ReadersLogin(String username,String password){
        //获取subject
        Subject subject = SecurityUtils.getSubject();
        //封装数据
        UserToken userToken = new UserToken(username, password, UserEnum.SELLER);
        //认证
        try {
            subject.login(userToken);
        } catch (UnknownAccountException e) {
            e.printStackTrace();
            return new Result(ResultConstant.USERNAME_PASSWORD_ERROR_CODE, ResultConstant.USERNAME_PASSWORD_ERROR_DATA);
        }catch (IncorrectCredentialsException e){
            e.printStackTrace();
            return new Result(ResultConstant.USERNAME_PASSWORD_ERROR_CODE, ResultConstant.USERNAME_PASSWORD_ERROR_DATA);
        }
        //单点登录
        //获取当前用户
        Readers readers = (Readers) subject.getPrincipal();
        //脱敏处理
        ReadersVO vo = new ReadersVO();
        BeanUtils.copyProperties(readers,vo);
        //生成token
        String to = jwtUtil.createJwt(vo.getId().toString(), JSONObject.toJSONString(readers), "role1");
        return new Result(ResultConstant.LOGIN_SUCCESS_CODE, ResultConstant.LOGIN_SUCCESS_DATA,to);
    }
}
